package texture;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class GLReferenceTexture {
	public static String getDefaultDomain(){
		return "minecraft";
	}
	
	public static int glGenTextures() {
		return GL11.glGenTextures();
	}

	public static void glBindTexture(int par0) {
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, par0);
	}

	public static void glTexImage2D(int width, int height, IntBuffer dataBuffer) {
		GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, dataBuffer);
	}

	private static int max_texture_size = -1;

	public static int glMaximumTextureSize() {
		if (max_texture_size != -1) {
			return max_texture_size;
		}

		for (int i = 16384; i > 0; i >>= 1) {
			GL11.glTexImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, i, i, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
			int j = GL11.glGetTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH);
			if (j != 0) {
				max_texture_size = i;
				return i;
			}
		}

		return -1;
	}

	public static void glTexSubImage2D(int xoffset, int yoffset, int width, int height, IntBuffer dataBuffer) {
		GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, xoffset, yoffset, width, height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, dataBuffer);
	}

	public static void glTextureClamped(boolean clampYN) {
		if (clampYN) {
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
		} else {
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
		}
	}

	public static void glTextureBlurred(boolean blurYN) {
		if (blurYN) {
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
		} else {
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		}

	}

	public static boolean anaglyph() {
		// Minecraft.getMinecraft().gameSettings.anaglyph
		return false;
	}

	public static synchronized ByteBuffer createDirectByteBuffer(int par0) {
		return ByteBuffer.allocateDirect(par0).order(ByteOrder.nativeOrder());
	}

	public static IntBuffer createDirectIntBuffer(int par0) {
		return createDirectByteBuffer(par0 << 2).asIntBuffer();
	}

	public static FloatBuffer createDirectFloatBuffer(int par0) {
		return createDirectByteBuffer(par0 << 2).asFloatBuffer();
	}
}
